package hhm.android.main

import android.view.View
import android.widget.*
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.launcher.ARouter
import hhm.android.base.fragment.NewLazyFragment

import hhm.android.library.bus.Bus
import hhm.android.library.utils.GlideUtils
import hhm.android.library.utils.OnMultiClickListener
import siau.android.base.*
import siau.android.library.KeyString


class FourFragment(var statusBarHeightPx: Int) : NewLazyFragment(), View.OnClickListener {

    constructor() : this(20)

    private lateinit var icon: ImageView
    private lateinit var message: ImageView
    private lateinit var scan: ImageView
    private lateinit var name: TextView
    private lateinit var identity: TextView
    private lateinit var invite: TextView
    private lateinit var buy: TextView
    private lateinit var lowView: View
    private lateinit var topView: View
    private lateinit var space: Space
    private lateinit var highView: View
    private lateinit var inviteView: ConstraintLayout
    private lateinit var myBaby: ConstraintLayout
    private lateinit var friendList: ConstraintLayout
    private lateinit var setting: LinearLayout
    private lateinit var iconRl: RelativeLayout
    private lateinit var nameLl: RelativeLayout

    override fun getContentViewId(): Int {
        return R.layout.fragment_four
    }

    override fun initView(view: View?) {
        super.initView(view)
        val title = view?.findViewById<TextView>(R.id.fragment_four_title)!!

        when {
            HttpHelper.isPrepare() -> {
                title.text = "${title.text} (预发布环境)"
            }
            HttpHelper.isInnerTest() -> {
                title.text = "${title.text} (测试17环境)"
            }
            HttpHelper.isOuterTest() -> {
                title.text = "${title.text} (测试环境(内网穿透))"
            }
        }

        space = view.findViewById(R.id.fragment_four_show_hide_space)!!
        lowView = view.findViewById(R.id.fragment_four_low_bg)!!
        highView = view.findViewById(R.id.fragment_four_high_bg)!!
        inviteView = view.findViewById(R.id.fragment_four_invite_view)!!
        invite = view.findViewById(R.id.fragment_four_invite)!!
        message = view.findViewById(R.id.fragment_four_message)!!
        scan = view.findViewById(R.id.fragment_four_scan)!!

        icon = view.findViewById(R.id.fragment_four_icon)!!
        name = view.findViewById(R.id.fragment_four_name)!!
        identity = view.findViewById(R.id.fragment_four_identity)!!
        myBaby = view.findViewById(R.id.fragment_four_my_baby)!!
        friendList = view.findViewById(R.id.fragment_four_friend_list)!!
        setting = view.findViewById(R.id.fragment_four_setting)!!
        buy = view.findViewById(R.id.fragment_four_no_data_buy)!!

        iconRl = view.findViewById(R.id.fragment_four_icon_rl)!!
        nameLl = view.findViewById(R.id.fragment_four_name_ll)!!

        topView = view.findViewById(R.id.fragment_four_top_view)
        val layoutParams = topView.layoutParams
        layoutParams.height = statusBarHeightPx
        topView.layoutParams = layoutParams
    }

    override fun initData() {
        super.initData()

        initInvite()
    }

    private fun initInvite() {
        name.text = SBApplication.userData.nickName
        identity.text = when (SBApplication.userData.identityType) {
            1 -> {
                getString(R.string.mother)
            }
            2 -> {
                getString(R.string.father)
            }
            else -> {
                SBApplication.userData.userIdentity
            }
        }
        if (SBApplication.userData.avatarUrl.isNullOrEmpty()) {
            iconRl.setBackgroundColor(activity?.let { ContextCompat.getColor(it, R.color.trans) }!!)
            icon.setImageResource(R.mipmap.take_picture)
        } else {
            iconRl.setBackgroundResource(R.drawable.ring_main_2)
            activity?.let { GlideUtils.loadCircleImage(it, SBApplication.userData.avatarUrl, icon, R.mipmap.take_picture, R.mipmap.take_picture) }
        }
        if (SBApplication.userData.isCreator == 1) {
            lowView.visibility = View.GONE
            space.visibility = View.GONE
            highView.visibility = View.VISIBLE
            inviteView.visibility = View.VISIBLE
        } else if (SBApplication.userData.isCreator == 0) {
            lowView.visibility = View.VISIBLE
            space.visibility = View.VISIBLE
            highView.visibility = View.GONE
            inviteView.visibility = View.GONE
        }
    }

    override fun getData() {
        super.getData()

        initInvite()

        if (SBApplication.userData.unreadCount == 0) {
            message.setImageResource(R.mipmap.message)
        } else {
            message.setImageResource(R.mipmap.message_red)
        }
    }

    override fun initEvent() {
        super.initEvent()
        buy.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (SystemUtil.isPkgInstalled(activity, "com.taobao.taobao")) {
                    SystemUtil.gotoShop(activity, "taobao://shop504445296.m.taobao.com")
                } else {
                    showToast("您还没有安装淘宝客户端！")
                }
            }
        })
        myBaby.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(BABY_LIST_ROUTE).navigation()
            }
        })
        friendList.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(FRIEND_LIST_ROUTE).navigation()
            }
        })
        setting.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(SETTING_ROUTER).navigation()
            }
        })
        invite.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(INVITE_ROUTE).navigation()
            }
        })
        scan.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
//                if (SBApplication.startCalculate.value == true) {
//                    SBApplication.startCalculate.value = false
//                }
                Bus.getDefault().post(KeyString.scanQRCode)
            }
        })
        message.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(MESSAGE_ROUTER).navigation()
            }
        })

        identity.setOnClickListener(this)
        iconRl.setOnClickListener(this)
        nameLl.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        ARouter.getInstance().build(MY_ROUTE).navigation()
    }
}